package b1.scenes.entry
{
	import b1.conifg.*;
	import b1.controls.basic.*;
	import b1.net.*;
	import b1.players.*;
	import b1.scenes.*;
	import b1.scenes.home.*;
	import b1.scenes.load.LoadingScene;
	import b1.scenes.main.*;
	import b1.scenes.prelude.Prelude;
	
	import flash.display.Sprite;
	import flash.events.*;
	import flash.filters.*;
	import flash.text.*;
	import flash.ui.Mouse;
		
	public class AnimalItem extends Sprite
	{
		private const glowColor:Array = [0x65A5B6, 0x8F6232, 0xD69588, 0xD91324, 0xEECF1F, 0xF0F0F0];  
		public var animalConfig:AnimalConfig;
		private var animalMc:AnimalMc;
		
		public function AnimalItem(animalConfig:AnimalConfig)
		{
			super();
			this.animalConfig = animalConfig;
			//创建临时虚拟动物
			var animalInfo:AnimalInfo = new AnimalInfo();
			animalInfo.id = -animalInfo.animalIndex;
			animalInfo.animalIndex = animalConfig.index;
			PlayerInfoHelper.getInstance().animals[animalInfo.id] = animalInfo;
			animalMc = new AnimalMc(animalInfo.id, 130);
			addChild(animalMc);
			animalMc.x = 65;
			animalMc.y = 100;
			animalMc.addEventListener(MouseEvent.MOUSE_OVER, onSelectorBackgroundMouseOver);
			animalMc.addEventListener(MouseEvent.MOUSE_OUT, onSelectorBackgroundMouseOut);
			addChild(animalMc);
			//动物名字
			var titleText:TextField = new TextField();
			titleText.selectable = false;
        	titleText.autoSize = TextFieldAutoSize.LEFT;
        	var format1:TextFormat = new TextFormat("微软雅黑", 20, 0xFFFFFF);
			format1.letterSpacing = 6;
			titleText.defaultTextFormat = format1;
			titleText.text = animalConfig.name;
			titleText.x = 30;
			titleText.y = animalMc.y + 20;
			titleText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			addChild(titleText);
			
			addEventListener(MouseEvent.CLICK, onAnimalMouseClick);
			addEventListener(MouseEvent.MOUSE_OVER, onAnimalMouseOver);
		}
		
		private function onAnimalMouseClick(event:MouseEvent):void
		{
			var pi:PlayerInfoHelper = PlayerInfoHelper.getInstance();
			var animalClues:Object = new Object();
			for (var clueIndex:String in animalConfig.animalClues)
			{
				var animalClue:AnimalClueConfig = animalConfig.animalClues[clueIndex];
				//随机化动物的属性
				var randomIndex:int = Math.random() * animalClue.clueValues.length; 
				animalClues[animalClue.clueIndex] = animalClue.clueValues[randomIndex];
			}
			var userName:String = PlayerInfoHelper.getInstance().rrUserName;
			var randIndex:int = Math.random() * Config.animalNames.length;
			var animalName:String = Config.animalNames[randIndex]; 
			ServerHelper.getInstance().addUserOwnAnimal(pi.userId, userName, animalConfig.camp, animalConfig.index, animalName, 0, 1, animalConfig.baseHealthPoint, 
				animalConfig.baseVitalityPoint, animalConfig.baseIntelligence, animalConfig.baseSpeed, 0, animalClues, onSelectAnimalComplete); 
		}
		
		private function onSelectAnimalComplete():void
		{
			var loadingScene:LoadingScene = new LoadingScene("正在加载片头动画……");
			SceneContainer.getInstance().loadScene(loadingScene);
			ResourceHelper.getInstance().startLoadPreludeResource(loadingScene.loadBar.updateProgress, onLoadingPreludeComplete);
		}
		
		private function onLoadingPreludeComplete():void
		{
			var SoundPrelude:Class = ResourceHelper.getInstance().getClassByName("SoundPrelude");
			var prelude:Prelude = new Prelude(onPlayPreludeComplete);
			SceneContainer.getInstance().loadScene(prelude, SoundPrelude);
		}
		
		private function onPlayPreludeComplete():void
		{
			var home:Home = new Home(PlayerInfoHelper.getInstance().userId, 0);
			SceneContainer.getInstance().loadScene(home);
		}
		
		private function onSelectorBackgroundMouseOver(event:MouseEvent):void
		{
			var gradientGlow:GradientGlowFilter = new GradientGlowFilter();
			gradientGlow.distance = 0;
			gradientGlow.angle = 45;
			gradientGlow.colors = [0xFFFFFF, glowColor[animalConfig.index]];
			gradientGlow.alphas = [0, 1];
			gradientGlow.ratios = [0, 255];
			gradientGlow.blurX = 10;
			gradientGlow.blurY = 10;
			gradientGlow.strength = 2;
			gradientGlow.quality = BitmapFilterQuality.HIGH;
			gradientGlow.type = BitmapFilterType.OUTER;
			animalMc.filters = [gradientGlow];
		}
		
		private function onSelectorBackgroundMouseOut(event:MouseEvent):void
		{
			Mouse.cursor = "auto";
			animalMc.filters = null;
		}
		
		private function onAnimalMouseOver(event:MouseEvent):void
		{
			Mouse.cursor = "button";
			var fe:FirstEntry = SceneContainer.getInstance().currentScene as FirstEntry;
			fe.descriptionText.text = animalConfig.description;
			fe.descriptionText.x = this.parent.x + 30;
			fe.descriptionText.y = this.parent.y + 170;
		}
	}
}